package com.dykj.fireFighting.server.base.analysis.service.impl;

import cn.hutool.core.util.StrUtil;
import com.dykj.fireFighting.api.base.analysis.entity.DeviceRes;
import com.dykj.fireFighting.api.base.analysis.entity.EarlyWarningResult;
import com.dykj.fireFighting.api.base.analysis.entity.TongHuanBiRes;
import com.dykj.fireFighting.api.base.analysis.service.IDeviceAnalysisService;
import com.dykj.fireFighting.common.core.service.impl.BaseServiceImpl;
import com.dykj.fireFighting.server.base.analysis.mapper.DeviceMapMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/*
 * 设备分析
 */
@Slf4j
@DubboService
public class DeviceAnalysisServiceImpl extends BaseServiceImpl<DeviceMapMapper, EarlyWarningResult> implements IDeviceAnalysisService {

    @Override
    public List<EarlyWarningResult> getEarlyWarningByYear(String beginTime, String endTime, String unitId, String centerId) {
        return this.baseMapper.getEarlyWarningByYear(beginTime,endTime,unitId,centerId);
    }

    @Override
    public List<EarlyWarningResult> getEarlyWarningByQuarter(String beginTime, String endTime, String unitId, String centerId) {
        return this.baseMapper.getEarlyWarningByQuarter(beginTime, endTime, unitId, centerId);
    }

    @Override
    public List<EarlyWarningResult> getEarlyWarningByMonth(String beginTime, String endTime, String unitId, String centerId) {
        return this.baseMapper.getEarlyWarningByMonth(beginTime, endTime, unitId, centerId);
    }

    @Override
    public List<EarlyWarningResult> getEarlyWarningByDay(String beginTime, String endTime, String unitId, String centerId) {
        return this.baseMapper.getEarlyWarningByDay(beginTime, endTime, unitId, centerId);
    }

    @Override
    public Integer getSumNumToday() {
        return this.baseMapper.getSumNumToday();
    }

    @Override
    public Integer getSumNumAll() {
        return this.baseMapper.getSumNumAll();
    }

    @Override
    public List<DeviceRes> getLevelByUnitIdAndTime(String moudleType,String unitIds, String beginTime, String endTime) {
        List<String> ids = null;
        if (StrUtil.isNotBlank(unitIds)){
            ids = Arrays.asList(unitIds.split(","));
        }
        return this.baseMapper.getLevelByUnitIdAndTime(moudleType,ids,beginTime,endTime);
    }

    @Override
    public List<DeviceRes> getTrendByUnitIdAndTime(String moudleType,String unitIds, String beginTime, String endTime, String timeGranularity) {
        List<String> ids = null;
        if (StrUtil.isNotBlank(unitIds)){
            ids = Arrays.asList(unitIds.split(","));
        }
        return this.baseMapper.getTrendByUnitIdAndTime(moudleType,ids,beginTime,endTime,timeGranularity);
    }

    @Override
    public List<DeviceRes> getAreaOrBusinessByTime(String moudleType, String beginTime, String endTime,String dataDimension) {
        return this.baseMapper.getAreaOrBusinessByTime(moudleType,beginTime,endTime,dataDimension);
    }

    @Override
    public List<DeviceRes> getAreaOrBusinessTrend(String moudleType, String beginTime, String endTime, String dataDimension, String timeGranularity) {
        return this.baseMapper.getAreaOrBusinessTrend(moudleType,beginTime,endTime,dataDimension,timeGranularity);
    }

    @Override
    public List<DeviceRes> getAreaOrBusinessRatio(String moudleType, String beginTime, String endTime, String dataDimension) {
        Integer sumAll = this.baseMapper.getAreaOrBusinessRatiod(moudleType, beginTime, endTime, dataDimension);
        if (null == sumAll || sumAll ==0) {
            return null;
        }
        List<DeviceRes> list =this.getAreaOrBusinessByTime(moudleType, beginTime, endTime, dataDimension);
        for (DeviceRes deviceRes : list) {
            int i = Integer.parseInt(deviceRes.getResData());
            DecimalFormat df = new DecimalFormat("0.00");
            String result = df.format((float) i / sumAll * 100);
            deviceRes.setResData(result);
        }
        return list;
    }

    @Override
    public List<TongHuanBiRes> getTongBiByTimeAndUnitId(String moudleType, String beginTime, String endTime, String timeGranularity,String unitIds) {
        List<TongHuanBiRes> list = new ArrayList<>();
        int beginYear =Integer.parseInt( beginTime.split("-")[0]);
        int endYear = Integer.parseInt(  endTime.split("-")[0]);
        int interval = endYear - beginYear;
        if (StrUtil.isBlank(unitIds)){
            String begin  = this.baseMapper.getTongBiByTime( moudleType,  beginTime,  timeGranularity);
            String end  = this.baseMapper.getTongBiByTime( moudleType,  endTime,  timeGranularity);
            TongHuanBiRes tongHuanBiRes = new TongHuanBiRes();
            tongHuanBiRes.setResBegin(begin);
            tongHuanBiRes.setResEnd(end);
            tongHuanBiRes.setResDateBegin(beginTime);
            tongHuanBiRes.setResDateEnd(endTime);
            list.add(tongHuanBiRes);
        }else {
            List<String> ids = Arrays.asList(unitIds.split(","));
            list = this.baseMapper.getTongBiByTimeAndUnitId(moudleType, beginTime, endTime,  timeGranularity,ids,interval);
        }
        
        return list;
    }

    @Override
    public List<TongHuanBiRes> getTongBiByTimeAndUnitIdByMonth(String moudleType, String beginTime, String endTime, String timeGranularity, String unitIds) {
        List<TongHuanBiRes> list = new ArrayList<>();
        int beginYear =Integer.parseInt( beginTime.split("-")[0]);
        int endYear = Integer.parseInt(  endTime.split("-")[0]);
        int interval = endYear - beginYear;
        if (StrUtil.isBlank(unitIds)){
            list  = this.baseMapper.getTongBiByTimeByMonth( moudleType, beginTime, endTime,  timeGranularity,interval);
        }else {
            List<String> ids = Arrays.asList(unitIds.split(","));
            list = this.baseMapper.getTongBiByTimeAndUnitIdByMonth(moudleType, beginTime, endTime,  timeGranularity,ids,interval);
        }
        return list;
    }

    @Override
    public List<TongHuanBiRes> getAreaOrBusinessTongBiByTime(String moudleType, String beginTime, String endTime, String timeGranularity, String dataDimension) {
        List<TongHuanBiRes> list = new ArrayList<>();
        int beginYear =Integer.parseInt( beginTime.split("-")[0]);
        int endYear = Integer.parseInt(  endTime.split("-")[0]);
        int interval = endYear - beginYear;
        list = this.baseMapper.getAreaOrBusinessTongBiByTime(moudleType, beginTime, endTime,  timeGranularity,dataDimension,interval);
        return list;
    }

    @Override
    public List<TongHuanBiRes> getAreaOrBusinessTongBiByTimeByMonth(String moudleType, String beginTime, String endTime, String timeGranularity, String dataDimension) {
        List<TongHuanBiRes> list = new ArrayList<>();
        int beginYear =Integer.parseInt( beginTime.split("-")[0]);
        int endYear = Integer.parseInt(  endTime.split("-")[0]);
        int interval = endYear - beginYear;
        list = this.baseMapper.getAreaOrBusinessTongBiByTimeByMonth(moudleType, beginTime, endTime,  timeGranularity,dataDimension,interval);
        return list;
    }

    @Override
    public List<TongHuanBiRes> getHuanBiByTimeAndUnitId(String moudleType, String beginTime, String endTime, String timeGranularity, String unitIds) {
        List<TongHuanBiRes> list = new ArrayList<>();
        if (StrUtil.isBlank(unitIds)){
            String begin  = this.baseMapper.getTongBiByTime( moudleType,  beginTime,  timeGranularity);
            String end  = this.baseMapper.getTongBiByTime( moudleType,  endTime,  timeGranularity);
            TongHuanBiRes tongHuanBiRes = new TongHuanBiRes();
            tongHuanBiRes.setResBegin(begin);
            tongHuanBiRes.setResEnd(end);
            tongHuanBiRes.setResDateBegin(beginTime);
            tongHuanBiRes.setResDateEnd(endTime);
            list.add(tongHuanBiRes);
        }else {
            List<String> ids = Arrays.asList(unitIds.split(","));
            list = this.baseMapper.getHuanBiByTimeAndUnitId(moudleType, beginTime, endTime,  timeGranularity,ids);
        }

        return list;
    }

    @Override
    public List<TongHuanBiRes> getAreaOrBusinessHuanBiByTime(String moudleType, String beginTime, String endTime, String timeGranularity, String dataDimension) {
        List<TongHuanBiRes> list = this.baseMapper.getAreaOrBusinessHuanBiByTime(moudleType,beginTime,endTime,timeGranularity,dataDimension);
        return list;
    }
}
